<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统运行环境检查</title>
    <link rel="icon" href="__INSTALL_STATIC__/css/LT.jpg">
    <link rel="stylesheet" href="__INSTALL_STATIC__/css/reset.css?random={extendSetting('random')}">
    <link rel="stylesheet" href="__INSTALL_STATIC__/css/common.css?random={extendSetting('random')}">
    <link rel="stylesheet" href="__INSTALL_STATIC__/css/step.css?random={extendSetting('random')}">
    <script src="__INSTALL_STATIC__/js/jquery-3.4.1.min.js?random={extendSetting('random')}"></script>
</head>
<body>
<div class="ltcms-wrapper">

        <section class="ltcms-section">
            <header>
                <div class="header-left">
                    <h1>LTCMS</h1>
                    <h3>程序初始化设置</h3>
                </div>
                <p class="header-right">
                    配置数据库连接参数
                </p>
            </header>
            <section class="ltcms-section1">
                <article class="article3">
                    <h1>MySQL数据库设置</h1>
                    <form id="dbForm" method="post">
                        <input type="hidden" name="step" value="checkdb"/>
                        <input type="hidden" class="force_install" name="force_install" value="0"/>
                        <table>
                            <tr>
                                <td>数据库地址：</td>
                                <td><input id="db_host" name="db_host" type="text" value="{if isset($dbInput['db_host']) }{$dbInput['db_host']}{else}localhost{/if}">通常为localhost或服务器IP地址</td>
                            </tr>
                            <tr>
                                <td>数据库名称：</td>
                                <td><input id="db_name" name="db_name" type="text" value="{if isset($dbInput['db_name']) }{$dbInput['db_name']}{/if}">服务器中建立的数据库的名称</td>
                            </tr>
                            <tr>
                                <td>数据表前缀：</td>
                                <td><input name="db_table_pre" type="text" value="{if isset($dbInput['db_table_pre']) }{$dbInput['db_table_pre']}{else}ltcms_{/if}">用于区分不同的数据表(可更改)</td>
                            </tr>
                            <tr>
                                <td>数据库用户：</td>
                                <td><input id="db_user" name="db_user" type="text" value="{if isset($dbInput['db_user']) }{$dbInput['db_user']}{else}root{/if}">数据库的用户名</td>
                            </tr>
                            <tr>
                                <td>数据库密码：</td>
                                <td><input id="db_pwd" name="db_pwd" type="text" value="{if isset($dbInput['db_pwd']) }{$dbInput['db_pwd']}{/if}">数据库的密码</td>
                            </tr>
                            <tr>
                                <td></td>
                                <td><p onclick="dbCheck(true)">测试数据库连接</p></td>
                            </tr>
                        </table>
                    </form>
                </article>
                <footer>
                    <div class="footer-left">
                        <progress value="60" max="100"></progress>
                        <span>60%</span>
                    </div>
                    <div class="buttons">
                        <form id="backTo" action="/install" method="post" >
                            <input type="hidden" class="step" name="step" value="3"/>
                        </form>
                        <form id="install" action="/install" method="post" onsubmit="return dbCheck(false,true);">
                            <input type="hidden" class="step" name="step" value="5"/>
                            <button type="button" onclick="goBack()" >上一步(P)</button>&nbsp;
                            <button >下一步(N)</button>&nbsp;
                            <button type="button" onclick="if(confirm('您确定要退出安装向导吗？')) window.close();" >取消(C)</button>
                        </form>
                    </div>
                </footer>
            </section>
        </section>
    {include 'install/script'}
    <script>
        /**
         * 测试数据库链接
         */
        function testDb(tc){
            var form = document.getElementById("dbForm");
            // 用表单来初始化
            var formData = new FormData(form);

            var ok=false;
            $.ajax({
                processData:false,
                contentType :false,
                async:false,
                cache: false,
                type:"post",
                url: "/install/exec",
                timeout: 5000,
                data:formData,
                dataType:"json",
                success:function(data)
                {
                    if(!(typeof data == "object"))
                    {
                        data=eval("("+data+")");
                    }
                    if(data.success){
                        if(tc){
                            alert("数据库设置正确，连接正常！");
                        }else{
                            ok=true;
                        }
                    }else{
                        alert(data.codeMsg);
                    }
                },
                error:function(xhr)
                {
                    alert("数据库测试失败");
                },
                complete:function(xhr)
                {

                }
            });
            return ok;
        }
        function dbCheck(tc,force_install) {
            var db_host = document.querySelector("#db_host");
            var db_user = document.querySelector("#db_user");
            var db_pwd = document.querySelector("#db_pwd");
            var db_name = document.querySelector("#db_name");
            var ok=true;
            if (db_host.value == "") {
                alert("请填写数据库地址");
                ok= false;
            } else if (db_user.value == "") {
                alert("请填写数据库用户");
                ok= false;
            } else if (db_pwd.value == "") {
                alert("请填写数据库密码");
                ok= false;
            } else if (db_name.value == "") {
                alert("请填写数据库名称");
                ok= false;
            }else{
                if(force_install){
                    $(".force_install").val(1);
                }else{
                    $(".force_install").val(0);
                }
                ok= testDb(tc);
            }
            return ok;
        }
    </script>
</div>
</body>
</html>